#coding=utf-8
from bisect import insort
import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path
import time
import inspect

# from datetime import datetimes

dir = Path.joinpath(Path(__file__).parent.parent,'logs')



dir_time = time.strftime("%Y-%m-%d",time.localtime())
handlers = {logging.NOTSET: Path.joinpath(dir,'notest_%s.log'%dir_time),
            logging.DEBUG: Path.joinpath(dir,'debug_%s.log'%dir_time),
            logging.INFO: Path.joinpath(dir,'info_%s.log'%dir_time),
            logging.WARNING: Path.joinpath(dir,'warning_%s.log'%dir_time),
            logging.ERROR: Path.joinpath(dir,'error_%s.log'%dir_time),
            logging.CRITICAL: Path.joinpath(dir,'critical_%s.log'%dir_time),
            }

def create_handlers():
    log_levels = handlers.keys()
    for level in log_levels:
        handlers[level] = RotatingFileHandler(handlers[level],maxBytes=100000,backupCount=3,encoding='utf-8')
        
        
create_handlers()

class Log():
    
    def __init__(self,level=logging.NOTSET):
        # 日志器对象
        self.__loggers = {}
        log_levels = handlers.keys()
        for level in log_levels:
            logger = logging.getLogger(str(level))
            logger.addHandler(handlers[level])
            logger.setLevel(level)
            self.__loggers.update({level: logger})
     
    def get_now(self):
        return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())     
      
    def fmt_message(self,level,message):
        filename = inspect.stack()[0].filename
        lineNo = inspect.stack()[0].lineno
        functionName = inspect.stack()[0].function
        return "[%s] [%s] [%s - %s - %s] %s"%(self.get_now(),level,filename,lineNo,functionName,message)
        
    def console_handle(self,level='DEBUG'):
        # 控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setLevel(level)
        # 添加格式
        console_handler.setFormatter(self.get_formatter()[0])
        return console_handler
        
    def info(self,message):
        message = self.fmt_message('info',message)
        self.__loggers[logging.INFO].info(message)
        
    def error(self,message):
        message = self.fmt_message('error',message)
        self.__loggers[logging.ERROR].error(message)
        
    def warning(self,message):
        message = self.fmt_message('warning',message)
        self.__loggers[logging.WARNING].warning(message)
        
    def debug(self,message):
        message = self.fmt_message('debug',message)
        self.__loggers[logging.DEBUG].debug(message)
        
    def critical(self,message):
        message = self.fmt_message('critical',message)
        self.__loggers[logging.CRITICAL].critical(message)
        
        
    def get_formatter(self):
        # 格式器
        console_fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_fmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        return console_fmt,file_fmt
        
    # def get_log(self):
    #     # 添加处理器
    #     self.logger.addHandler(self.console_handle())
    #     self.logger.addHandler(self.info_file_handle())
    #     self.logger.addHandler(self.error_file_handle())
    #     return self.logger
    
if __name__ == "__main__":
    logger = Log()
    logger.info('this is a info message')
    logger.warning('this is a warning message')
    logger.error('this is a error message')